package payment.simulator.institution.tx.realgathering;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import payment.api.tx.realgathering.Tx2051Request;

public class Tx2051 extends HttpServlet {

    private static final long serialVersionUID = -2314166584340483329L;

    private static final Logger logger = Logger.getLogger("system");

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.取得参数
            String institutionID = request.getParameter("InstitutionID");
            String applyTxSN = request.getParameter("ApplyTxSN");
            String bankID = request.getParameter("BankID");
            String cardType = request.getParameter("CardType");
            String accountName = request.getParameter("AccountName");
            String accountNumber = request.getParameter("AccountNumber");
            String identificationType = request.getParameter("IdentificationType");
            String identificationNumber = request.getParameter("IdentificationNumber");
            String phoneNumber = request.getParameter("PhoneNumber");
            String payType = request.getParameter("PayType");
            String authNo = request.getParameter("AuthNo");
            String amountLimit = request.getParameter("AmountLimit");
            String timeStart = request.getParameter("TimeStart");
            String timeEnd = request.getParameter("TimeEnd");
            String timeUnit = request.getParameter("TimeUnit");
            String timeStep = request.getParameter("TimeStep");
            String remark = request.getParameter("Remark");
            
            // 2.创建交易请求对象
            Tx2051Request tx2051Request = new Tx2051Request();
            tx2051Request.setInstitutionID(institutionID);
            tx2051Request.setApplyTxSN(applyTxSN);
            tx2051Request.setBankID(bankID);
            tx2051Request.setCardType(cardType);
            tx2051Request.setAccountName(accountName);
            tx2051Request.setAccountNumber(accountNumber);
            tx2051Request.setIdentificationType(identificationType);
            tx2051Request.setIdentificationNumber(identificationNumber);
            tx2051Request.setPhoneNumber(phoneNumber);
            tx2051Request.setPayType(payType);
            tx2051Request.setAuthNo(authNo);
            tx2051Request.setAmountLimit(amountLimit);
            tx2051Request.setTimeStart(timeStart);
            tx2051Request.setTimeEnd(timeEnd);
            tx2051Request.setTimeUnit(timeUnit);
            tx2051Request.setTimeStep(timeStep);
            tx2051Request.setRemark(remark);

            // 3.执行报文处理
            tx2051Request.process();
            logger.debug("[plainText]=[" + tx2051Request.getRequestPlainText() + "]");

            // 4.将参数放置到request对象
            request.setAttribute("plainText", tx2051Request.getRequestPlainText());
            request.setAttribute("message", tx2051Request.getRequestMessage());
            request.setAttribute("signature", tx2051Request.getRequestSignature());
            request.setAttribute("txCode", "2051");
            request.setAttribute("txName", "委托协议签约申请（发短信）");
            request.setAttribute("action", request.getContextPath() + "/SendMessage");

            // 5.转向Request.jsp页面
            request.getRequestDispatcher("/Request.jsp").forward(request, response);

        } catch (Exception e) {
            logger.error("", e);
            processException(request, response, e.getMessage());
        }
    }

    private void processException(HttpServletRequest request, HttpServletResponse response, String errorMessage) {
        try {
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
        } catch (Exception e) {
        }
    }
}
